<template>
  <a-modal
    :width="1000"
    :visible="visible"
    title="喷淋头管理"
    :body-style="{ backgroundColor: '#F0F2F5', padding: '8px' }"
    :footer="null"
    @update:visible="updateVisible"
  >
    <div class="ele-body">
      <!-- 搜索表单 -->
      <div class="block-interval">
        <a-card :bordered="false">
          <a-form layout="inline" :model="where">
            <a-row>
              <a-form-item label="牧云汇平台ID:">
              <a-input v-model:value.trim="where.hzaxxId" placeholder="请输入牧云汇平台ID" allow-clear />
            </a-form-item>
              <a-form-item class="ele-text-center">
                <a-space>
                  <a-button type="primary" @click="reload">查询</a-button>
                  <a-button @click="reset">重置</a-button>
                </a-space>
              </a-form-item>
        
            </a-row>
          </a-form>
        </a-card>
      </div>

      <!-- 表格 -->
      <div>
          <a-card :bordered="false">
            <ele-pro-table
            ref="table"
            row-key="sprinklerId"
            :datasource="datasource"
            :columns="columns"
            :where="where"
            :scroll="{ x: 'max-content' }"
          > 
            <template #bodyCell="{ column, record }">
              <!-- table操作栏按钮 -->
              <template v-if="column.key === 'action'">
                <a-space>
                  <a @click="openEdit(record)">修改</a>
                  <a-divider type="vertical" />
                  <a-popconfirm title="确定要删除此记录吗？" @confirm="remove(record)">
                    <a class="ele-text-danger">删除</a>
                  </a-popconfirm>
                </a-space>
              </template>

              <!-- 其他列的模板 -->
              <template v-if="column.key === 'onOffStatusColumn'">
                <a-tag v-if="record.onlineStatus == 1" color="green">开</a-tag>
                <a-tag v-else color="red">关</a-tag>
              </template>


            </template>
          </ele-pro-table>
        </a-card>
      </div>
    </div>
  </a-modal>
</template>

<script>
import { DeviceAiEdgeSpraySprinklerApi } from '@/api/property/aiedgecomputer/DeviceAiEdgeSpraySprinklerApi';
import { message } from 'ant-design-vue';
export default {
  name: 'DeviceAiedgecomputerSpraySprinkler',
  components: {
  },
  emits: ['done', 'update:visible'],
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 菜单数据
    data: Object
  },
  async mounted() {
  },
  data() {
    return {
      // 表格列配置
      columns: [
        {
          title: '设备id',
          dataIndex: 'sprinklerId'
        },
        {
          title: '牧云汇平台ID',
          dataIndex: 'hzaxxId'
        },
        {
          title: '设备名称',
          dataIndex: 'name'
        },
        {
          title: '开关状态',
          dataIndex: 'onOffStatus',
          key : 'onOffStatusColumn'
        },
        {
          title: '状态更新时间',
          dataIndex: 'onOffStatusTime'
        }
      ],
      // 表格搜索条件
      where: {
        delFlag : 'N',
        sprayComputerId : this.data.sprayComputerId
      },
    };
  },
  methods: {
    /**
     * 搜索按钮
     *
     * @author cancan
     * @date 2025/06/16 22:15
     */
    reload() {
      this.selection = [];
      this.$refs.table.reload({ page: 1 });
    },

    /**
     * 重置搜索
     *
     * @author cancan
     * @date 2025/06/16 22:15
     */
    reset() {
      this.where.hzaxxId = '';
      this.where.delFlag = 'N';
      this.where.sprayComputerId = this.data.sprayComputerId;
      this.$nextTick(() => {
        this.reload();
      });
    },

    /**
     * 删除
     *
     * @author cancan
     * @date 2025/06/16 22:15
     */
    async remove(row) {
      const result = await DeviceAiEdgeSpraySprinklerApi.delete({ sprinklerId: row.sprinklerId });
      message.success(result.message);
      this.reload();
    },

    /**
     * 打开新增或编辑弹窗
     *
     * @author cancan
     * @date 2025/06/16 22:15
     */
    openEdit(row) {
      this.current = row;
      this.showEdit = true;
    },
    updateVisible(value) {
      this.$emit('update:visible', value);
    },
    /**
     * 获取表格数据
     *
     * @author fengshuonan
     * @date 2022/5/8 15:18
     */
     datasource({ page, limit, where, orders }) {
      return DeviceAiEdgeSpraySprinklerApi.findPage({ ...where, ...orders, pageNo: page, pageSize: limit });
    }
  }
};
</script>